package controller;

import model.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.IManagerService;
import service.IOrderService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("order")
public class OrderController {
    @Autowired
    private IOrderService iOrderService;
    private IManagerService iManagerService;

    @RequestMapping("query")
    public ModelAndView query(Order order){
        int totalCount = iOrderService.totalCount(order);
        System.out.println("总行数："+totalCount);
        int totalPage;
        if (totalCount % order.getRowCount() == 0) {
            totalPage = (int) totalCount / order.getRowCount();

        } else {
            totalPage = (int) totalCount / order.getRowCount() + 1;
        }
        ModelAndView mv = new ModelAndView();
        List<Order> list = iOrderService.query(order);
        mv.addObject("list", list);
        mv.addObject("pageNo", order.getPageNo());
        mv.addObject("totalPage", totalPage);
        mv.setViewName("manager/order_list");
        return  mv;
    }

    @RequestMapping("queryByOrderNo")
    public ModelAndView queryByOrderNo(Order order){
        System.out.println("订单号是:"+order.getOrderNo());
        ModelAndView mv = new ModelAndView();
        if(order.getOrderStatus()==null && order.getuName()==null){
            if(iOrderService.query(order).size()!=0){
                Order obj = iOrderService.query(order).get(0);
                List<Order> list = iOrderService.listByOrderNo(order);
                mv.addObject("obj", obj);
                mv.addObject("list", list);
            }
        }
        mv.setViewName("manager/order_details");
        return  mv;
    }

    @RequestMapping("updateOrderStatus")
    @ResponseBody
    public String updateOrderStatus(Order order){
        return  iOrderService.updateOrderStatus(order);
    }

    @RequestMapping("shipment")
    public ModelAndView shipment(String orderNo){
        if(orderNo.length()!=0  && orderNo!=null){
            Map<String,String> map = new HashMap<>();
            map.put("orderNo", orderNo);
            iOrderService.shipment(map);
        }
        return query(new Order());


    }

    @RequestMapping("refund")
    public ModelAndView refund(String orderNo){
        if(orderNo.length()!=0  && orderNo!=null){
            Map<String,String> map = new HashMap<>();
            map.put("orderNo", orderNo);
            iOrderService.refundMoney(map);
        }
        return iOrderService.listRefund();
    }

    @RequestMapping("refundPage")
    public ModelAndView refundPage(Order order){
        ModelAndView mv = new ModelAndView();
        mv.addObject("list", iOrderService.query(order));
        mv.setViewName("manager/order_refund");
        return  mv;
    }
    @RequestMapping("sign")
    @ResponseBody
    public String sign(Order order){
        return iOrderService.sign(order);
    }
}
